var api          = require('API').rank,
    get_all_role = require('API').rbac.roles;

module.exports = ['$scope', function ($scope) {

    $scope.all_roles = [];

    (function () {
        get_all_role(function (json) {
            $scope.$apply(function () {
                $scope.all_roles = json;
            });
        })
    })();
    $scope.edit = function(item){
        $scope.edit_open = true;
        $scope.cur_rank = item;
    }

    function refresh(cb) {
        api.all(function (json) {
            $scope.$apply(function () {
                $scope.add_title_show = false;
                $scope.add_cover = false;
                $scope.ranks = json;
                $scope.new_rank = {};
                $scope.cur_rank = {};
                $scope.cur_rank_unassigned_roles = [];
                $scope.cur_rank_assigned_roles = [];
                //ranks role
                $scope.ranks_role_title = true;
                $scope.ranks_role_cover = true;
                if (cb)cb();
            });
        })
    }

    refresh();

    $scope.add_show = function () {
        $scope.add_title_show = true;
        $scope.add_cover = true;
        $scope.ranks_role_title = false;
        $scope.ranks_role_cover = false;
    };

    //add submit
    $scope.add = function () {
        api.add($scope.new_rank, function () {
            alert("岗位添加成功！");
            refresh();
        });
    };

    $scope.ranks_role_show = function (item) {
        $scope.add_title_show = false;
        $scope.add_cover = false;
        $scope.ranks_role_title = true;
        $scope.ranks_role_cover = true;
        var data = $scope.ranks;
        //修改颜色
        if (item.iscolor) {
            item.iscolor = false;
        } else {
            for (var i = 0; i < data.length; i++) {
                data[i].iscolor = false;
            }
            item.iscolor = true;
        }

        $scope.cur_rank = item;

        api.roles_for_rank($scope.cur_rank, function (roles) {
            $scope.$apply(function () {
                $scope.cur_rank_assigned_roles = roles;
                $scope.cur_rank_unassigned_roles =
                    $scope.all_roles.slice(0).filter(function (r) {
                        return !roles.some(function (x) {
                            return x.role_id == r.id
                        })
                    });
            });
        })

    };

    $scope.del_role = function (role) {
        var save = $scope.cur_rank;
        console.log('role_to_del', role);
        api.remove_role_from_rank(role, save, function () {
            $scope.$apply(function () {
                refresh(function () {
                    $scope.ranks_role_show(save);
                });
            });
        });
    };
    $scope.add_role = function (role) {
        var save = $scope.cur_rank;
        api.add_role_to_rank(role, save, function () {
            $scope.$apply(function () {
                refresh(function () {
                    $scope.ranks_role_show(save);
                });
            });
        });
    };

    $scope.upd = function(){
        var save = $scope.cur_rank;
        api.upd(save, function () {
            $scope.$apply(function () {
                alert("岗位信息修改成功！");
                refresh(function () {
                    $scope.ranks_role_show(save);
                });
            });
        });
    };

    //delete submit
    $scope.del = function (item) {
        if (confirm("确定删除吗!")) {
            api.del(item.id, function () {
                $scope.$apply(function () {
                    refresh();
                });
            })
        }
    }


}];